import 'package:flutter/material.dart';
import './Pages/AuthPage.dart';
import './Pages/ProductShow/ProductPage.dart';
import './Pages/ProductShow/DetailPage.dart';

import 'package:scoped_model/scoped_model.dart';
import './ScopedModels/ScopedMain.dart';

//应用入口
void main(List<String> args) => runApp(MyApp());

//创建一个root widget
//可变 Widget
//从StatelessWidget 切换到 StatefulWidget 需要重启 而不是热加载

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
   final ScopedMain model = ScopedMain();
    return ScopedModel<ScopedMain>(
        model: model,
        child: MaterialApp(
          theme: new ThemeData(primaryColor: Colors.deepOrange),
          // home: AuthPage(),//首页路由'/'
          routes: {
            '/': (BuildContext context) => AuthPage(),
            '/products': (BuildContext context) => ProductPage(model),
 
          },
          onGenerateRoute: (RouteSettings settings) {
            final List<String> pathElments =
                settings.name.split('/'); //'/product/2'
            if (pathElments[0] != '') {
              return null;
            }
            if (pathElments[1] == 'product') {
              final int index = int.parse(pathElments[2]);
              return MaterialPageRoute<bool>(
                  builder: (BuildContext context) => DetailPage(index));
            }
          },
          onUnknownRoute: (RouteSettings settings) {
            return MaterialPageRoute(
                builder: (BuildContext context) => ProductPage(model));
          },
        ));
  }
}
